/* -*- c++ -*- */
/*
 * Copyright 2004 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

/*
 * config.h is generated by configure.  It contains the results
 * of probing for features, options etc.  It should be the first
 * file included in your .cc file.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <ftw_ofdm_cmap_cc.h>
#include <gr_io_signature.h>
#include <iostream>
#include <stdexcept>
#include <string.h>

/*
 * Create a new instance of ftw_ofdm_cmap_cc and return
 * a boost shared_ptr.  This is effectively the public constructor.
 */
ftw_ofdm_cmap_cc_sptr 
ftw_make_ofdm_cmap_cc (int fft_size, int tones){
  return ftw_ofdm_cmap_cc_sptr (new ftw_ofdm_cmap_cc (fft_size , tones));
}

/*
 * Specify constraints on number of input and output streams.
 * This info is used to construct the input and output signatures
 * (2nd & 3rd args to gr_block's constructor).  The input and
 * output signatures are used by the runtime system to
 * check that a valid number and type of inputs and outputs
 * are connected to this block.  In this case, we accept
 * only 1 input and 1 output.
 */
static const int MIN_IN = 1;	// mininum number of input streams
static const int MAX_IN = 1;	// maximum number of input streams
static const int MIN_OUT = 1;	// minimum number of output streams
static const int MAX_OUT = 1;	// maximum number of output streams

/*
 * The private constructor
 */
ftw_ofdm_cmap_cc::ftw_ofdm_cmap_cc (int fft_size, int tones) // tones should be 48 subcarriers + 4 pilots + dc = 53
  : gr_block ("ofdm_cmap_cc",
	      gr_make_io_signature (MIN_IN, MAX_IN, tones * sizeof (gr_complex)),
	      gr_make_io_signature (MIN_OUT, MAX_OUT, fft_size * sizeof (gr_complex))),
	      d_fft_size(fft_size),
	      d_tones(tones)
{
  
}

/*
 * Our virtual destructor.
 */
ftw_ofdm_cmap_cc::~ftw_ofdm_cmap_cc ()
{
  // nothing required
}

int ftw_ofdm_cmap_cc::general_work (int                        noutput_items,
			       gr_vector_int                   &ninput_items,
			       gr_vector_const_void_star       &input_items,
			       gr_vector_void_star             &output_items)
{
  const gr_complex *in = (const gr_complex *) input_items[0];
  gr_complex *out = (gr_complex *) output_items[0]; 
  int counter=0;
  int i=0; 
 
  //gr_complex *app = new gr_complex(0);

  /*
        * Sanity check
  */
  if(d_fft_size != 64){
    throw std::invalid_argument("ftw_ofdm_cmap_cc: For IEEE 802.11p fft_length must be 64 ");
  } 
   
  while(counter < noutput_items) {
    for (i = 0; i <= 26; i++)        
      out[i + (counter * d_fft_size)] = in[i+26 + (counter * d_tones)];
    for (i = 27 ; i <= 37 ; i++)
      out[i + (counter * d_fft_size)] = 0;
    for (i = 38; i < d_fft_size ; i++)
      out[i + (counter * d_fft_size)] = in[i-38 + (counter * d_tones)];
    counter++;
  }

  // Tell runtime system how many input items we consumed on
  // each input stream.

  consume_each (noutput_items);

  // Tell runtime system how many output items we produced.
  return noutput_items;
}
